//
//  ZXHomeSectionView.m
//  ZiJinLian
//
//  Created by 震霄 张 on 2018/3/9.
//  Copyright © 2018年 lzy. All rights reserved.
//

#import "ZXHomeSectionView.h"
#import "SDCycleScrollView.h"
#import "ScrollPartView.h"
#import "ZXPartGoodsView.h"
#import "HomeTopAdvModel.h"
#import "HomeAdverModel.h"
#import "ZXCustomeCate.h"
#import "ZXCulturePartView.h"
#import "HomeCultureModel.h"
#import "HomeQualityModel.h"
#import "ZXChoicePartView.h"
#import "ZXHomeHeadView.h"
#import "ZXHomeHeadView.h"
@interface ZXHomeSectionView()<SDCycleScrollViewDelegate,UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *cycscroHdis;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollViewH;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *advcusH;


@property (weak, nonatomic) IBOutlet NSLayoutConstraint *requestViewH;

@property (weak, nonatomic) IBOutlet UIScrollView *newsScrollView;//资讯

@property (weak, nonatomic) IBOutlet UIView *advCusView;
@property (nonatomic,weak)SDCycleScrollView *topAdvView;
@property (nonatomic,weak)SDCycleScrollView *cusAdvView;
@property (weak, nonatomic) IBOutlet UIView *requestPartBgView;
@property (weak, nonatomic) IBOutlet UIView *culPartView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *culPartViewH;
@property (weak, nonatomic) IBOutlet UIScrollView *qualityScrollView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *qualityViewH;
@property (weak, nonatomic) IBOutlet UIScrollView *NEWscrollView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *NEWScrollH;

@property (weak,nonatomic)ZXHomeHeadView *headView;
@property (weak, nonatomic) IBOutlet UIView *ZXheadView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *zxHeadViewH;


@property (weak, nonatomic) IBOutlet UILabel *zixunLabel;
@property (weak, nonatomic) IBOutlet UILabel *whdyLabel;
@property (weak, nonatomic) IBOutlet UILabel *jinpinLabel;
@property (weak, nonatomic) IBOutlet UILabel *xinpinLabel;


@end

@implementation ZXHomeSectionView

+(instancetype)zxhomeSectionView
{
	return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil].lastObject;
}
-(void)awakeFromNib
{
	[super awakeFromNib];
	self.autoresizingMask = UIViewAutoresizingNone;
	self.cycscroHdis.constant = (screen_Width) /16*9;
	self.scrollViewH.constant = (screen_Width-40)/16*9+70;
	self.advcusH.constant =(screen_Width-50)*0.4;
	
	[self setupUI];
}
-(void)setupUI
{
	SDCycleScrollView *sectionScrollerView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, MainW, MainW/16*9) delegate:self placeholderImage:[UIImage imageNamed:@"banner_bj"]];
	sectionScrollerView.bannerImageViewContentMode=UIViewContentModeScaleAspectFill;
//	sectionScrollerView.imageURLStringsGroup = @[@"banner_bj",@"fo"];  // 网络图片
	//self.sectionScrollerView.localizationImageNamesGroup =array1 ; // 本地图片
	sectionScrollerView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
	sectionScrollerView.autoScrollTimeInterval =3.;
	[self addSubview:sectionScrollerView];
	self.topAdvView = sectionScrollerView;
	
	SDCycleScrollView *sdView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, (MainW-50), (MainW-50)*0.4) delegate:self placeholderImage:[UIImage imageNamed:@"banner_bj"]];
	sdView.bannerImageViewContentMode=UIViewContentModeScaleAspectFill;
	//	sectionScrollerView.imageURLStringsGroup = @[@"banner_bj",@"fo"];  // 网络图片
	//self.sectionScrollerView.localizationImageNamesGroup =array1 ; // 本地图片
	sdView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
	sdView.autoScrollTimeInterval =3.;
	[self.advCusView addSubview:sdView];
	self.cusAdvView = sdView;
	

}

-(void)setModel:(HomeZXAllModel *)model
{
	_model = model;
	NSMutableArray *tmparr = [NSMutableArray array];
	for (int i = 0; i< model.indexModuleHomeAdv.data.count; i++) {
		HomeTopAdvModel *advModel = model.indexModuleHomeAdv.data[i];
		[tmparr addObject:advModel.imageUrl];
	}
	self.topAdvView.imageURLStringsGroup = [tmparr copy];
	
	//创建资讯
	[self.newsScrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
	self.newsScrollView.contentSize = CGSizeMake(model.indexModuleEvent.data.count*(MainW-30),self.scrollViewH.constant);
	self.newsScrollView.showsHorizontalScrollIndicator = NO;
	self.newsScrollView.delegate = self;
	for (int j = 0; j<model.indexModuleEvent.data.count; j++) {
		ScrollPartView *partView = [ScrollPartView scrollPartView];
		CGFloat partW = MainW-40;
		CGFloat partH = self.scrollViewH.constant;
		partView.frame = CGRectMake(10+j*(partW+10), 0, partW, partH);
		partView.model = model.indexModuleEvent.data[j];
		[self.newsScrollView addSubview:partView];
	}
	self.zixunLabel.text = self.model.indexModuleEvent.name;
	
	//自定义广告
	NSMutableArray *tmparr1 = [NSMutableArray array];
	for (int m = 0; m<model.indexModuleCateAdv.data.count; m++) {
		HomeAdverModel *advcusmodel = model.indexModuleCateAdv.data[m];
		[tmparr1 addObject:advcusmodel.imageUrl];
	}
	self.cusAdvView.imageURLStringsGroup = [tmparr1 copy];
	
	//心愿请求部分
	[self.requestPartBgView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

	CGFloat requestPartH = 70;
	CGFloat requestPartW = (MainW-50)/4;
	NSInteger num = model.indexModuleCustomCate.data.count % 4 == 0 ? model.indexModuleCustomCate.data.count/4:model.indexModuleCustomCate.data.count/4+1;
	self.requestViewH.constant = requestPartH * num;//设置心愿请求部分的视图高度
	NSInteger cout = model.indexModuleCustomCate.data.count;
	for (int n = 0; n<cout; n++) {
		HomeRequstModel *requestModel = model.indexModuleCustomCate.data[n];
		ZXCustomeCate *cusView = [ZXCustomeCate zxcustomeCate];
		cusView.model = requestModel;
		cusView.frame = CGRectMake((n%4)*requestPartW, (n/4)*requestPartH, requestPartW, requestPartH);
		[self.requestPartBgView addSubview:cusView];
	}
	
	//文化底蕴
	[self.culPartView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

	CGFloat whdyPartH = MainW/16*9+160+10+10;
	self.culPartViewH.constant = whdyPartH * model.indexModuleCulture.data.count;
	for (int k = 0; k<model.indexModuleCulture.data.count; k++) {
		HomeCultureModel *culModel = model.indexModuleCulture.data[k];
		ZXCulturePartView *cV = [ZXCulturePartView zxculturePartView];
		cV.model = culModel;
		cV.frame = CGRectMake(0, k*whdyPartH, MainW, whdyPartH);
		[self.culPartView addSubview:cV];
	}
	self.whdyLabel.text = self.model.indexModuleCulture.name;
	
	//精品首推
	[self.qualityScrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

	self.qualityViewH.constant = (MainW-50)/16*9 + 60;
	for (int q = 0; q<model.indexModuleChoice.data.count; q++) {
		HomeQualityModel *qM = model.indexModuleChoice.data[q];
		ZXChoicePartView *qv = [ZXChoicePartView zxchoicePartView];
		qv.model = qM;
		qv.frame = CGRectMake(q*MainW, 0, MainW, self.qualityViewH.constant);
		[self.qualityScrollView addSubview:qv];
	}
	self.qualityScrollView.contentSize = CGSizeMake(MainW * model.indexModuleChoice.data.count, self.qualityViewH.constant);
	self.jinpinLabel.text = self.model.indexModuleChoice.name;
	
	//新品发布
	[self.NEWscrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

	self.NEWScrollH.constant =(screen_Width-40)/16*9+70;
	self.NEWscrollView.contentSize = CGSizeMake(model.indexModuleNew.data.count*(MainW-30), self.NEWScrollH.constant);
	self.NEWscrollView.showsHorizontalScrollIndicator = NO;
	for (int k = 0; k<model.indexModuleNew.data.count; k++) {
		ScrollPartView *partView = [ScrollPartView scrollPartView];
		partView.isNEW = YES;
		CGFloat partW = MainW-40;
		CGFloat partH = self.NEWScrollH.constant;
		partView.frame = CGRectMake(10+k*(partW+10), 0, partW, partH);
		partView.NEWModel = model.indexModuleNew.data[k];
		[self.NEWscrollView addSubview:partView];
	}
	self.xinpinLabel.text = self.model.indexModuleNew.name;
	
	//商品推荐
	[self.ZXheadView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

	self.zxHeadViewH.constant = model.indexModuleCate.data.count>0? 110+(MainW-50)*0.4:0;
	ZXHomeHeadView *headView = [ZXHomeHeadView zxhomeHeadView];
	headView.frame = CGRectMake(0, 0, MainW, self.zxHeadViewH.constant);
	[self.ZXheadView addSubview:headView];
	self.headView = headView;
	self.headView.model = model.indexModuleCate.data[0];
	
}



//-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
//{
//	CGFloat partw = MainW-40;
////	NSInteger currentPage = scrollView.contentOffset.x/MainW;
////	self.newsScrollView.
//	scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x/MainW, 0);
//
//}

/** 点击图片回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
	//跳转网页
	NSString *url = @"";
	if (cycleScrollView == self.topAdvView) {
		HomeTopAdvModel *model = self.model.indexModuleHomeAdv.data[index];
		url = model.linkUrl;
		
	}else if (cycleScrollView == self.cusAdvView){
		HomeAdverModel *model = self.model.indexModuleCateAdv.data[index];
		url = model.imageLink;
	}
	
	if (url.length>0) {
		NSMutableDictionary *dic = [NSMutableDictionary dictionary];
		[dic setValue:@"1" forKey:@"type"];
		[dic setValue:url forKey:@"value"];
		[[NSNotificationCenter defaultCenter]postNotificationName:@"jumpOther" object:nil userInfo:dic];
	}


	
}

/** 图片滚动回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index
{
	
}


//-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
//{
//	if (scrollView == self.newsScrollView) {
//		CGFloat distance = scrollView.contentOffset.x;
//		NSInteger cout = 0;
//		while (distance>MainW-40) {
//			distance -= MainW-40;
//			cout++;
//		}
//		scrollView.contentOffset = CGPointMake(10+(MainW-40+10)*cout, 0);
//
//	}
//}
@end
